package net.xdclass.controller;

import com.amazonaws.Request;
import net.xdclass.controller.req.FileUpdateReq;
import net.xdclass.controller.req.FolderCreateReq;
import net.xdclass.dto.AccountFileDTO;
import net.xdclass.interceptior.LoginInterceptor;
import net.xdclass.service.AccountFileService;
import net.xdclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/file/v1")
public class AccountFileController {

    @Autowired
    private AccountFileService accountFileService;

    @GetMapping("/list")
    public JsonData list(@RequestParam("parent_id") Long parentId) {

        Long accountId = LoginInterceptor.threadLocal.get().getId();

        List<AccountFileDTO> list = accountFileService.listFile(accountId,parentId);

        return JsonData.buildSuccess(list);
    }

    /**
     * 创建文件夹
     */
    @PostMapping("/create_folder")
    public JsonData createFolder(@RequestBody FolderCreateReq folderCreateReq) {
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        folderCreateReq.setAccountId(accountId);
        accountFileService.createFolder(folderCreateReq);
        return JsonData.buildSuccess();
    }

    /**
     * 文件重命名
     */
    @PostMapping("rename_file")
    public JsonData renameFile(@RequestBody FileUpdateReq req){
        Long accountId = LoginInterceptor.threadLocal.get().getId();
        req.setAccountId(accountId);
        accountFileService.renameFile(req);
        return JsonData.buildSuccess();
    }
}
